<?php

/**
 * Controller_group
 * 权限管理
 */


class Controller_group extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->grade = YOU::loadModel('grade');
		$this->model->class = YOU::loadModel('class');
		$this->model->menu  = YOU::loadModel('menu');
	}

	/** 首页 **/
	public function actionIndex ()
	{


	}


	/** 组列表 **/
	public function actionList()
	{
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_group`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$group_lists = $this->db->fetchAll("SELECT * FROM `tbl_group` ORDER BY group_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($group_lists as $key => $value){
			$group_lists[$key]['user_count']  = count(unserialize($value['group_staff']));
		}


		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("group_lists", $group_lists);
		$this->tpl->display("group/group.list.tpl.php");
	}

	/** 组添加 **/
	public function actionAdd()
	{
		if ($_POST){

			$group_name = $this->post->group_name;

			$info = array(
			"group_name"  => $group_name,
			"status"      => 1,
			"create_time" => date("Y-m-d H:i:s"),
			"update_time" => "0000-00-00 00:00:00",
			);
			$this->db->do_insert("tbl_group", $info);


			// 跳转
			Helper_Client::location("添加成功", "index.php?a=group.list");


		}
		$this->tpl->display("group/group.add.tpl.php");
	}

	/** 组修改 **/
	public function actionUpdate()
	{
		$group_id = (int)$this->get->group_id;

		$group_data = $this->db->fetchRow("SELECT * FROM `tbl_group` WHERE group_id='{$group_id}'");
		if (empty($group_data)){
			Helper_Client::alert("用户组不存在");
		}

		if ($_POST){

			$group_name = $this->post->group_name;

			$info = array(
			"group_name"  => $group_name,
			"update_time" => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_group", $info, "group_id='{$group_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=group.list");

		}

		$this->tpl->assign("group_data", $group_data);
		$this->tpl->display("group/group.update.tpl.php");
	}

	/**  组删除 **/
	public function actionDel()
	{
		$group_id = (int)$this->get->group_id;

		$sql = "DELETE FROM `tbl_group` WHERE group_id='{$group_id}'";
		$this->db->query($sql);

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=group.list");

	}

	/** 组成员管理 **/
	public function actionUser()
	{
		$group_id  = $this->get->group_id;
		$user_type = $this->get->user_type;
		if (empty($user_type) || !in_array($user_type, array("staff", "student"))){
			$user_type = "staff";
		}
		$group_data = $this->db->fetchRow("SELECT group_{$user_type} AS group_user FROM `tbl_group` WHERE group_id='{$group_id}'");
		if (empty($group_data)){
			Helper_Client::alert("用户组不存在");
		}
		$group_user = unserialize($group_data['group_user']);

		if ($_POST){
			$pid = $this->post->pid;

			foreach ((array)$pid as $key => $value){
				if (substr($value, 0, 1) == "u"){
					$user_id = intval(substr($value, 1));
					if (!in_array($user_id, $group_user)){
						// 加入新的
						$group_user[] = $user_id;
					}
				}
			}
			// 序列化后保存
			$group_user = serialize($group_user);

			$info = array(
			"group_{$user_type}" => $group_user,
			);
			$this->db->do_update("tbl_group", $info, "group_id='{$group_id}'");

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=group.list");
		}


		foreach ((array)$group_user as $key => $value){
			$check[$value]++;
		}

		$this->tpl->assign("group_id", $group_id);

		if ($user_type == "student"){

			// 年级列表
			$grade_lists = $this->model->grade->getAll();
			foreach ($grade_lists as $key => $value){
				$grade[$value['grade_id']] = $value['grade_name'];
			}

			// 班级列表
			$class_lists = $this->model->class->getAll();
			foreach ($class_lists as $key => $value){
				$class[$value['class_id']] = $value['class_name'];
			}


			// 学生按 年级 班级 列出
			$student_lists = $this->db->fetchAll("SELECT a.user_id,b.student_name,b.class_id,b.grade_id FROM `tbl_user` AS a,`tbl_student` AS b WHERE a.type_id=b.student_id");
			foreach ($student_lists as $key => $value){
				$grade_tree[$value['grade_id']] = $grade[$value['grade_id']];
				$class_tree[$value['class_id']]['grade_id'] = $value['grade_id'];
				$class_tree[$value['class_id']]['name'] = $class[$value['class_id']];
				if ($check[$value['user_id']] && !empty($check[$value['user_id']])){
					$student_lists[$key]['is_check'] = 1;
				}
			}

			$this->tpl->assign("grade_tree", $grade_tree);
			$this->tpl->assign("class_tree", $class_tree);
			$this->tpl->assign("student_lists", $student_lists);
			$this->tpl->display("group/group.student.tpl.php");
		}

		if ($user_type == "staff"){
			// 学生按 年级 班级 列出
			$staff_lists = $this->db->fetchAll("SELECT a.user_id,b.staff_name,b.staff_nature FROM `tbl_user` AS a,`tbl_staff` AS b WHERE a.type_id=b.staff_id");
			foreach ($staff_lists as $key => $value){
				$nature_tree[$value['staff_nature']] = $this->staff_nature_ini[$value['staff_nature']];
				if ($check[$value['user_id']] && !empty($check[$value['user_id']])){
					$staff_lists[$key]['is_check'] = 1;
				}
			}

			$this->tpl->assign("nature_tree", $nature_tree);
			$this->tpl->assign("staff_lists", $staff_lists);
			$this->tpl->display("group/group.staff.tpl.php");
		}


	}

	/** 组用户列表 **/
	public function actionUserList()
	{
		$group_id = $this->get->group_id;

		$group_data = $this->db->fetchRow("SELECT * FROM `tbl_group` WHERE group_id='{$group_id}'");
		if (empty($group_data)){
			Helper_Client::alert("用户组不存在");
		}
		
		
	}
	
	/** 组权限管理 **/
	public function actionPms()
	{
		$group_id = $this->get->group_id;

		$group_data = $this->db->fetchRow("SELECT * FROM `tbl_group` WHERE group_id='{$group_id}'");
		if (empty($group_data)){
			Helper_Client::alert("用户组不存在");
		}


		if ($_POST){
			$pid = $this->post->pid;
			$group_pms = serialize($pid);

			$info = array(
			"group_pms" => $group_pms,
			);
			$this->db->do_update("tbl_group", $info, "group_id='{$group_id}'");

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=group.list");
		}

		// 用户组权限
		$group_pms = unserialize($group_data['group_pms']);
		foreach ((array)$group_pms as $key => $value){
			$pms[$value]++;
		}

		// 读出所有的菜单方法
		$pms_lists = $this->model->menu->getAll();
		foreach ($pms_lists as $key => $value){
			if ($pms[$value['menu_id']] && !empty($pms[$value['menu_id']])){
				$pms_lists[$key]['is_check'] = 1;
			}
		}

		$this->tpl->assign("pms_lists", $pms_lists);
		$this->tpl->display("group/group.pms.tpl.php");
	}




}



?>